var nowApplicationName = null;

function intoApplicationList() {
	if (nowProjectIndex >= 0) {
		getApplicationList(nowProjectIndex);
		listDivKind = "application";
		openListDiv();
	}
}


function getApplicationList() {
	send("GET", "/@ServerApplication?" + nowProjectIndex, true, "", showApplicationList, null);
}

function removeApplication(fileName) {
	if (confirm("是否删除应用\n" + "\"" + fileName + "\"")) {
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "Remove";
		json.fileName = fileName;
		send("POST", "/@ServerApplication", true, JSON.stringify(json), showApplicationList, null);
	}
}

function getApplication(fileName) {
	nowApplicationName = fileName;
	var json = {};
	json.projectIndex = nowProjectIndex;
	json.order = "Get";
	json.fileName = fileName;
	send("POST", "/@ServerApplication", true, JSON.stringify(json), showApplicationCode, null);
	document.getElementById("CodeChange").onclick = changeApplicationCode;
	openCodeDiv();
}

function changeApplicationCode() {
	if (nowApplicationName != null) {
		var code = editor.getValue();
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "Change";
		json.fileName = nowApplicationName;
		json.context = code;
		send("POST", "/@ServerApplication", true, JSON.stringify(json), showApplicationCode, null);
	}
}

function setApplicationId(fileName) {
	var str = prompt("请输入新定位", "NewID");
	if (str != null) {
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "SetId";
		json.fileName = fileName;
		json.context = str;
		send("POST", "/@ServerApplication", true, JSON.stringify(json), showApplicationList, null);
	}
}

function addApplication() {
	var str = prompt("请输入新应用名", "NewApplication");
	if (str != null) {
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "Add";
		json.fileName = str;
		send("POST", "/@ServerApplication", true, JSON.stringify(json), showApplicationList, null);
	}
}

function showApplicationList(text) {
	var ApplicationList = JSON.parse(text);
	var html = "";
	for (let i = 0; i < ApplicationList.length; i++) {
		html += "<lable>标识</lable>";
		html += "<input type=\"button\" name=\"id\" class=\"name\" onclick=\"setApplicationId('" + ApplicationList[i]
			.applicationName + "')\" value=\"";
		html += ApplicationList[i].id;
		html += "\"/>";
		html += "<lable>应用</lable>";
		html += "<input type=\"button\" name=\"name\" class=\"name\" onclick=\"getApplication('" + ApplicationList[i]
			.applicationName + "')\" value=\"";
		html += ApplicationList[i].applicationName;
		html += "\"/>";
		html += "<button type=\"button\" class=\"remove\" onclick=\"removeApplication('" + ApplicationList[i].applicationName +
			"')\">删</button>";
		html += "<br>";
	}
	html += "<br><button type=\"button\" class=\"add\" onclick=\"addApplication()\">添加</button>";
	document.getElementById("ListTitle").innerHTML = "应用列表";
	document.getElementById("ListContent").innerHTML = html;
}

function showApplicationCode(text) {
	document.getElementById("CodeTitle").innerHTML = "" + nowApplicationName;
	var code = JSON.parse(text);
	editor.setValue(code);
}
